home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Developer.java < prev    next >
Text File  |  1998-10-28  |  6KB  |  203 lines

  1. /*
  2.  * Title: Conversing Applets
  3.  * Type: Applet
  4.  * Source: Developer.java
  5.  * Application Description:
  6.  * The Conversing Applets project provides a simple and effective way
  7.  * to allow applets to communicate within the same "environment", i.e.
  8.  * within the same browser page.
  9.  *
  10.  * This is done using a Mediator class as the background manager and
  11.  * other visible classes which display results. The Mediator class uses
  12.  * static variables to keep track of all the events taking place in
  13.  * these visible applets.
  14.  *
  15.  * In this example, two visible applets are used along with the one
  16.  * Mediator class. The Developer and Client class generate text strings which
  17.  * represent conversation.  The Mediator object(s) take those text
  18.  * strings and distribute them to the other applet (Developer to Client and
  19.  * Client to Developer).
  20.  *
  21.  * The result is two distinct applet objects transmitting data back and
  22.  * forth. This sort of project can be very useful in developing more
  23.  * interesting Web pages as well as Web page applets which gather
  24.  * information about usage, etc.  The Mediator class concept can be
  25.  * built upon to meet the needs of more specific Java-based Web
  26.  * applications.
  27.  *
  28.  * Symantec Corporation.
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.*;
  33.  
  34. /**
  35.  * The Developer applet class
  36.  */
  37.  
  38. public class Developer extends Applet implements Runnable
  39. {
  40.         Mediator myMediator;            //Local copy of the Mediator object
  41.         String label, response;         //Various applet text variables
  42.         int theNumber;                  //Used to track a random number
  43.         Thread developerThread;               //The main applet thread
  44.  
  45.         /**
  46.          * Set up the applet layout and initialize variables
  47.          */
  48.  
  49.         public void init()
  50.         {
  51.            myMediator = new Mediator();
  52.  
  53.            //{{INIT_CONTROLS
  54.         setLayout(null);
  55.         setSize(452,110);
  56.         top.setText("I\'m a Developer");
  57.         top = new java.awt.Label("I'm a Developer");
  58.         top.setBounds(0,0,113,24);
  59.         developer.setText("Developer says:");
  60.         add(top);
  61.         developer = new java.awt.Label("Developer says:");
  62.         developer.setBounds(0,35,109,24);
  63.         add(developer);
  64.         developerTalk = new java.awt.TextField(18);
  65.         developerTalk.setEditable(false);
  66.         developerTalk.setBounds(130,36,307,24);
  67.         client.setText("The Client just said:");
  68.         add(developerTalk);
  69.         client = new java.awt.Label("The Client just said:");
  70.         client.setBounds(0,70,126,24);
  71.         add(client);
  72.         clientTalk = new java.awt.TextField(18);
  73.         clientTalk.setEditable(false);
  74.         clientTalk.setBounds(130,68,309,24);
  75.         add(clientTalk);
  76.         //}}
  77.  
  78.  
  79.         }
  80.  
  81.         /**
  82.          * Start the main thread
  83.          */
  84.  
  85.         public void start()
  86.         {
  87.            developerThread = new Thread(this);
  88.            developerThread.start();
  89.         }
  90.  
  91.  
  92.          /**
  93.           * The applet action takes place here. The applet waits, gets a
  94.           * random number from the Mediator, uses that to id a piece of
  95.           * conversation and then updates the UI elements.
  96.           */
  97.  
  98.          public void run()
  99.          {
  100.             Thread developerThread = Thread.currentThread();
  101.             while(true)
  102.             {
  103.                 try
  104.                 {
  105.                     developerThread.sleep(2000);
  106.                 } catch(Exception e)
  107.                 {
  108.                     System.out.println(e);
  109.                 }
  110.  
  111.                 //Keep a copy of the last random number
  112.                 myMediator.oldNumber = myMediator.theNumber;
  113.                 myMediator.theNumber = myMediator.genRandom();
  114.  
  115.                 checkWithMediator();
  116.  
  117.                 takeAction();
  118.  
  119.             }
  120.          }
  121.  
  122.          /**
  123.           * A helper function which uses the new Mediator random number
  124.           * to set the conversation text
  125.           */
  126.  
  127.          public synchronized void checkWithMediator()
  128.          {
  129.  
  130.             int index = myMediator.theNumber;
  131.  
  132.             if(index < 10)
  133.             {
  134.                 label = "Have you seen Visual CafΘ?";
  135.             } else if((index <= 20) && (index >= 10))
  136.             {
  137.                 label = "What's that?";
  138.             } else if((index <= 30) && (index > 20))
  139.             {
  140.                 label = "Visual CafΘ is Great!";
  141.             } else if((index <= 40) && (index > 30))
  142.             {
  143.                 label = "Visual CafΘ really helps.";
  144.             } else if((index <= 50) && (index > 40))
  145.             {
  146.                 label = "I can make anything with Visual CafΘ.";
  147.             } else if((index <= 60) && (index > 50))
  148.             {
  149.                 label = "The Visual CafΘ compiler is sooo FAST!";
  150.             } else if((index <= 70) && (index > 60))
  151.             {
  152.                 label = "Debugging in Visual CafΘ is easy.";
  153.             } else if((index <= 80) && (index > 70))
  154.             {
  155.                 label = "I see where you're going";
  156.             } else if((index <= 90) && (index > 80))
  157.             {
  158.                 label = "Check out the snazzy class editor.";
  159.             } else if((index <= 100) && (index >90))
  160.             {
  161.                 label = "Thank you!";
  162.             }
  163.  
  164.             //Let the Mediator know what the developer said
  165.             myMediator.setDeveloperTalk(label);
  166.  
  167.             //Find out what the client said
  168.             response = myMediator.getClientTalk();
  169.          }
  170.  
  171.  
  172.          /**
  173.           * A helper method which updates the UI elements
  174.           */
  175.  
  176.          public void takeAction()
  177.          {
  178.             developerTalk.setText(label);
  179.             clientTalk.setText(response);
  180.          }
  181.  
  182.         //{{DECLARE_CONTROLS
  183.     java.awt.Label top = new java.awt.Label();
  184.     java.awt.Label developer = new java.awt.Label();
  185.     java.awt.TextField developerTalk = new java.awt.TextField(18);
  186.     java.awt.Label client = new java.awt.Label();
  187.     java.awt.TextField clientTalk = new java.awt.TextField(18);
  188.     //}}
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.